package com.niit.session19;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo {
	public static void main(String[] args) {
//		1.下载驱动并加载到build path
//		右键项目new-->folder 命名为lib
//		把mysql-connector-java-8.0.27.jar拖到lib文件夹
//		右键mysql-connector-java-8.0.27.jar选择build path-->add to build path
		Connection connection=null;
		Statement statement =null;
		ResultSet rs = null;
		try {
//		2.加载驱动程序
			Class.forName("com.mysql.cj.jdbc.Driver");
//			3.创建链接
			String username = "root";
			String password = "root";
			connection = DriverManager.getConnection("jdbc:mysql://localhost/student_2021",username,password);
//			4.指向SQL语句，获得结果集
			statement = connection.createStatement();
			rs = statement.executeQuery("select * from student where batch = 3");
			while (rs.next()) {
				System.out.println("学号："+rs.getString("sid")+"名字："+rs.getString("name"));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				connection.close();
				statement.close();
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
